/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.master.demo.im.service.impl;

import com.eatthepath.pushy.apns.ApnsClient;
import com.eatthepath.pushy.apns.ApnsClientBuilder;
import com.eatthepath.pushy.apns.ApnsPushNotification;
import com.eatthepath.pushy.apns.util.ApnsPayloadBuilder;
import com.eatthepath.pushy.apns.util.SimpleApnsPayloadBuilder;
import com.eatthepath.pushy.apns.util.SimpleApnsPushNotification;
import com.eatthepath.pushy.apns.util.TokenUtil;
import com.farsunset.cim.model.Message;
import com.master.common.validator.ValidatorUtil;
import com.master.demo.im.config.properties.APNsProperties;
import com.master.demo.im.service.IAPNsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

/**
 * 苹果apns推送接口 实现类
 *
 * @author: Yang
 * @date: 2022/07/05
 * @version: 1.0.0
 * Copyright Ⓒ 2022 Master Computer Corporation Limited All rights reserved.
 */
@Slf4j
@Service
public class APNsServiceImpl implements IAPNsService {
    private final ApnsClient apnsClient;
    private final APNsProperties properties;

    @Autowired
    public APNsServiceImpl(APNsProperties properties) throws IOException {
        this.properties = properties;
        if (ValidatorUtil.isNotNull(properties) && ValidatorUtil.isNotNull(properties.getP12File())) {
            InputStream stream = getClass().getResourceAsStream(properties.getP12File());
            apnsClient = new ApnsClientBuilder()
                    .setApnsServer(properties.isDebug() ? ApnsClientBuilder.DEVELOPMENT_APNS_HOST : ApnsClientBuilder.PRODUCTION_APNS_HOST)
                    .setClientCredentials(stream, properties.getP12Password())
                    .build();
        } else {
            apnsClient = null;
        }

    }

    @Override
    public void push(Message message, String deviceToken) {
        if (StringUtils.isBlank(deviceToken)) {
            return;
        }
        ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
        payloadBuilder.setAlertTitle("您有一条新的消息");

        payloadBuilder.setSound("default");
        payloadBuilder.setBadgeNumber(1);
        payloadBuilder.addCustomProperty("id", message.getId());
        payloadBuilder.addCustomProperty("action", message.getAction());
        payloadBuilder.addCustomProperty("content", message.getContent());
        payloadBuilder.addCustomProperty("sender", message.getSender());
        payloadBuilder.addCustomProperty("receiver", message.getReceiver());
        payloadBuilder.addCustomProperty("format", message.getFormat());
        payloadBuilder.addCustomProperty("extra", message.getExtra());
        payloadBuilder.addCustomProperty("timestamp", message.getTimestamp());

        String token = TokenUtil.sanitizeTokenString(deviceToken);
        String payload = payloadBuilder.build();

        ApnsPushNotification notification = new SimpleApnsPushNotification(token, properties.getAppId(), payload);
        apnsClient.sendNotification(notification).whenComplete((response, cause) -> {
            if (response != null) {
                log.info("APNs push done.\ndeviceToken : {} \napnsPayload : {}", deviceToken, payload);
            } else {
                log.error("APNs push failed", cause);
            }
        });
    }

}
